#include <stdio.h>
#include <string.h>

#define MAXFILENAME	20
#define TRUE		1
#define FALSE		0

void genname(char orig[], char name[])
{
	int i;
	int befdot = TRUE;

	for (i = 0; orig[i]!='\0'; i++){
		if(befdot){
			if (orig[i] != '.'){
				name[i] = orig[i];	
			} 
			else {
				befdot = FALSE;
				name[i]='\0';
				strcat(name, "_s.");
			}
		}
		else{  /* dot founded and added*/
			name[i+2] = orig[i];	 /* 3 is the size of "_s." */
		}
	}
	name[i+2]='\0';
}
int main(int argc, char* argv[]){
	FILE *orig, *copy;
	char c, oldc;
	int j, insidecom = FALSE;
	char name[MAXFILENAME], blank = ' ';

	if (argc <  2){
		printf("Usage: %s <file 1> <file 2> <file n>\n", argv[0]);
		return (1);
	}
	else{ /* file names have been supplied */
		for (j = 1; j < argc; j++){
			if ((orig = fopen(argv[j], "r")) == NULL){
				printf ("Error while opening the file %s\n", argv[j]);
				return (2);
			}
			else {
				genname(argv[j], name);
				if ((copy = fopen(name, "w")) == NULL){
					printf ("Error while opening the file %s\n", argv[j]);
					return (2);
				}
				oldc = ' ';
				while ((c = fgetc(orig)) != EOF){
					if (c == '*'){
						if (oldc == '/'){
							insidecom = TRUE;
						}
					}
					if (!insidecom){
						fputc(c, copy);
					} else {
						fputc(blank, copy);
						if (c == '/'){
							if (oldc == '*'){
								insidecom = FALSE;
							}		
						}
					}
					oldc = c;
				}
			}
			fclose(orig);
			fclose(copy);
		}
	}
}
